﻿Imports Microsoft.VisualBasic

Public Class MenuBuilder

    Private Shared _menuList As List(Of Menu)

    Public Shared Function GenerateMenu() As String

        Dim Menu As New StringBuilder
        Dim webControl As New Control
        Dim currentFolder As String = GetCurrentFolder()

        For Each section As Menu In GetListOfSections()

            If currentFolder.ToLower = section.Folder.ToLower Then
                Menu.Append(String.Format("<li class=""selected""><a href=""{0}"">{1}</a></li>", webControl.ResolveUrl(section.URL), section.Name))
            Else
                Menu.Append(String.Format("<li><a href=""{0}"">{1}</a></li>", webControl.ResolveUrl(section.URL), section.Name))
            End If

        Next

        Return Menu.ToString

    End Function

    Public Shared Function GenerateSubMenu() As String

        Dim Menu As New StringBuilder
        Dim webControl As New Control
        Dim currentFolder As String = GetCurrentFolder()

        For Each section As Menu In GetListOfSections()

            If currentFolder.ToLower = section.Folder.ToLower Then
                For Each SubMenu In section.SubMenu

                    Menu.Append(String.Format("<li><a href=""{0}"">{1}</a></li>", SubMenu.URL, SubMenu.Name))

                Next                
            End If

        Next

        Return Menu.ToString

    End Function

    Public Shared Function GetListOfSections() As List(Of Menu)

        If _menuList Is Nothing Then

            _menuList = New List(Of Menu)

            Dim aMenu As Menu
            aMenu = New Menu("Order", "Orders", "~/Order/OrderList.aspx")
            aMenu.SubMenu.Add(New SubMenu("Create Order", "CreateOrder.aspx"))
            aMenu.SubMenu.Add(New SubMenu("List Orders", "OrderList.aspx"))
            _menuList.Add(aMenu)

            aMenu = New Menu("Customer", "Customers", "~/Customer/CustomerList.aspx")
            aMenu.SubMenu.Add(New SubMenu("Add Customer", "CustomerDetail.aspx"))
            aMenu.SubMenu.Add(New SubMenu("List Customers", "CustomerList.aspx"))
            _menuList.Add(aMenu)

            aMenu = New Menu("Product", "Products", "~/Product/ProductList.aspx")
            aMenu.SubMenu.Add(New SubMenu("Add Product", "ProductDetail.aspx"))
            aMenu.SubMenu.Add(New SubMenu("List Products", "ProductList.aspx"))
            _menuList.Add(aMenu)

        End If

        Return _menuList
    End Function

    Public Shared Function GetCurrentFolder() As String

        Dim filePath As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath
        Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(filePath)
        Dim currentDirectory As String = fileInfo.DirectoryName
        Dim currentFolder As String = String.Empty

        If currentDirectory.Contains("\") Then
            currentFolder = Right(currentDirectory, currentDirectory.Length - (currentDirectory.LastIndexOf("\") + 1))
        End If

        Return currentFolder
    End Function

End Class
